<?php
class JoinController extends Controller_Helper
{
    public function indexAction()
    {
        $className = get_class();
        $classType = strtolower(preg_replace('/Controller/', '', $className));
        $this->_getSpecialInfo($classType);
        $this->view->char = Zend_Registry::get('lang')->model->$classType;

        //如果用户提交了表单
        if ($_POST['submit']) {
        	$joinTable = Zend_Registry::get('database')->modeljoin;
        	$modelInfo = array();
        	$modelInfo[$joinTable->name] = $this->_request->getParam($joinTable->name);
        	$modelInfo[$joinTable->gender] = $this->_request->getParam($joinTable->gender);
        	$modelInfo[$joinTable->country] = $this->_request->getParam($joinTable->country);
        	$modelInfo[$joinTable->address] = $this->_request->getParam($joinTable->address);
        	$modelInfo[$joinTable->detail] = $this->_request->getParam($joinTable->detail);
        	$modelInfo[$joinTable->telephone] = $this->_request->getParam($joinTable->telephone);
        	$modelInfo[$joinTable->language] = $this->_request->getParam($joinTable->language);
        	$modelInfo[$joinTable->worktime] = $this->_request->getParam($joinTable->worktime);
        	$modelInfo[$joinTable->email] = $this->_request->getParam($joinTable->email);
        	$modelInfo[$joinTable->im] = $this->_request->getParam($joinTable->im);
        	$modelInfo[$joinTable->upload] = $this->_request->getParam($joinTable->upload);
        	$modelInfo[$joinTable->prefer] = $this->_request->getParam($joinTable->prefer);
        	$modelInfo[$joinTable->know] = $this->_request->getParam($joinTable->know);

        	$switch = false;
        	$dealInfo = array();
        	foreach ($modelInfo as $key => $value) {
        		$dealInfo[$key] = strip_tags($value);
        		if (!empty($value)) {
        			$switch = true;
        		}
        	}

        	if ($switch) {
        	    //如果有信息就处理图片
        	    $pictures = $_FILES[$joinTable->upload]['name'];
        	    $pictures = $this->_decoratePictures($pictures);
        	    if (!empty($pictures)) {
        	        $addresses = $this->_decoratePictures($_FILES[$joinTable->upload]['tmp_name']);
        	    }
        	    $rtn = $this->_dealModelPictures($dealInfo, $pictures, $addresses);
        	} else {
        		$rtn = array(
        			'errorcode' => -1,
        			'errormsg' => $this->view->char->alert1
        		);
        	}
        	$this->view->result = $rtn;
        }
    }
    
    protected function _decoratePictures($pictures)
    {
        $rtn = array();
        foreach ($pictures as $picture) {
            if (!empty($picture)) {
                $rtn[] = $picture;
            }
        }
        return $rtn;
    }
    
    protected function _dealModelPictures($modelInfo, $pictures, $addresses)
    {
        $char = Zend_Registry::get('lang')->model->join;
        //检验所有的照片是否合法
        $switch = true;
        foreach ($pictures as $picture) {
            $type = array(
                "jpg",
                "gif",
                "jpeg",
                "png"
            );
            $tool = substr(strrchr($picture, '.'), 1);
            if (!in_array(strtolower($tool), $type)) {
                $rtn = array(
                    'errorcode' => -2,
                    'errormsg' => $char->alert2
                );
                $switch = false;
                break;
            }
        }
        //如果合法就保存
        if ($switch) {
            $joinTable = Zend_Registry::get('database')->modeljoin;
            $path = APPLICATION_PUBLIC_PATH . '/images/join/';
            $insertToDb = true;
            $pics = array();
            $len = count($pictures);
            for ($i = 0; $i < $len; $i++) {
                // 把上传的文件名以"."好为准做一个数组。
                $filename = explode(".", $pictures[$i]);
                // 取文件名，用时间戳
                $filename[0] = time() . '-' . ($i + 1);
                // 上传后的文件名
                $name = implode(".", $filename);
                $uploadfile = $path . $name;
                if (move_uploaded_file($addresses[$i], PROJECT_ROOT . $uploadfile)) {
                    //图片保存成功之后插入数据库
                    $modelInfo[$joinTable->upload][] = $uploadfile;
                } else {
                    $rtn = array(
                        'errorcode' => -3,
                        'errormsg' => $char->alert3
                    );
                    $insertToDb = false;
                    break;
                }
            }
            if ($insertToDb) {
                $modelInfo[$joinTable->upload] = serialize($modelInfo[$joinTable->upload]);
                $rtn = Business_Model::getInstance()->saveModelJoin($modelInfo);
            }
        }
        return $rtn;
    }
}